import {Component} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {IonicPage, NavController, ToastController} from 'ionic-angular';

import {User} from '../../providers/providers';
import {MainPage} from '../pages';
import {TabsPage} from '../tabs/tabs';
import {Uid} from '@ionic-native/uid';
import {AndroidPermissions} from '@ionic-native/android-permissions';
import {Api} from "../../providers/api/api";
import {userResult} from "../../models/response";
import { Device } from '@ionic-native/device';
import {ListMasterPage} from "../list-master/list-master";

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html'
})

export class LoginPage {

  // The account fields for the login form.
  // If you're using the username field with or without email, make
  // sure to add it to the type
  account: { name: string, password: string,mac: string } = {
    name: "",
    password: "",
    mac:"22",
  };
  // Our translated text strings
  private loginErrorString: string;

  constructor(public navCtrl: NavController,
              public user: User,
              public toastCtrl: ToastController,
              public translateService: TranslateService,
              private uid: Uid,
              private device: Device,
              private androidPermissions: AndroidPermissions,
              private api: Api) {

    this.translateService.get('LOGIN_ERROR').subscribe((value) => {
      this.loginErrorString = value;
    })
  }

  // Attempt to login in through our User service
  async doLogin() {
    const hasPermission = this.androidPermissions.checkPermission(
      this.androidPermissions.PERMISSION.READ_PHONE_STATE
    );

    if (!hasPermission) {
      const result = this.androidPermissions.requestPermission(
        this.androidPermissions.PERMISSION.READ_PHONE_STATE
      );

      if (!result['hasPermission']) {
        throw new Error('Permissions required');
      }

      // ok, a user gave us permission, we can get him identifiers after restart app
      return;
    }

    // //console.log(this.uid.MAC);

    if (this.account.name === '' || this.account.password === '') {
      let toast = this.toastCtrl.create({
        message: '用户名或密码不能为空！',
        duration: 3000,
        position: 'top'
      });
      toast.present();
      return false;
    }else{
      this.getList()
    }

    // let resp = await this.api._post<any, userResult>('User/UserLogin', this.account);
    //
    // if (resp.loginType === 0) {
    //   this.navCtrl.push(MainPage);
    // } else {
    //   let toast = this.toastCtrl.create({
    //     message: resp.loginType.toString(),
    //     duration: 3000,
    //     position: 'top'
    //   });
    //   toast.present();
    // }
  }



  list;
  uuid;
  getList(){
    this.uuid=this.device.uuid;
    // this.navCtrl.setRoot(ListMasterPage);
    var param = {name:this.account.name,password:this.account.password,mac:this.uuid}
    this.api.getList(param).subscribe(res=>{
      this.list = res;
      if(this.list.msg==""){
        sessionStorage.setItem("email", this.list.email);
        sessionStorage.setItem("userid", this.list.userid);
        sessionStorage.setItem("username", this.list.username);
        sessionStorage.setItem("pwd", this.account.password);
        this.navCtrl.push(MainPage);
      }else{
        let toast = this.toastCtrl.create({
          message: this.list.msg,
          duration: 3000,
          position: 'top'
        });
        toast.present();
        return false;
      }
    })
  }
}
